using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Common;
using MainView.Windows;
using SExperiment;
using SSettings;

namespace MainView.MainControls
{
	public partial class QuickExperimentListControl : UserControl, IComponentConnector, IStyleConnector
	{
		public enum ManageChangedType
		{
			None = -1,
			Delete = 1,
			Import = 2,
			Export = 3,
			DisplayMode = 4,
			Open = 5
		}

		public class QuickExperimentManageChangedEventArgs : RoutedEventArgs
		{
			public ManageChangedType type = ManageChangedType.None;

			public QuickExperimentData qd;

			public QuickExperimentManageChangedEventArgs(RoutedEvent routedEvent, object source)
				: base(routedEvent, source)
			{
			}
		}

		public static readonly RoutedEvent OnQuickExperimentManageChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnQuickExperimentManageChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(QuickExperimentListControl));

		private CollectionViewSource view = new CollectionViewSource();

		public event RoutedEventHandler ManageChanged
		{
			add
			{
				AddHandler(OnQuickExperimentManageChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnQuickExperimentManageChangedRoutedEvent, value);
			}
		}

		public QuickExperimentListControl()
		{
			InitializeComponent();
			InitQuickExperimentList();
			base.Loaded += QuickExperimentListControl_Loaded;
		}

		private void QuickExperimentListControl_Loaded(object sender, RoutedEventArgs e)
		{
			foreach (QuickExperimentData quickExperimentData in ConfigSettings.GetInstance().QuickExperimentDatas)
			{
				SetImageSource(quickExperimentData, (Experiment)quickExperimentData.Experiment);
			}
		}

		private void SetImageSource(QuickExperimentData qd, Experiment experiment)
		{
			if (experiment != null && experiment.Image != null)
			{
				try
				{
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.BeginInit();
					bitmapImage.StreamSource = new MemoryStream(experiment.Image);
					bitmapImage.EndInit();
					qd.ImageSource = bitmapImage;
				}
				catch (Exception)
				{
				}
			}
		}

		private void InitQuickExperimentList()
		{
			ObservableCollection<QuickExperimentData> quickExperimentDatas = ConfigSettings.GetInstance().QuickExperimentDatas;
			view.Source = quickExperimentDatas;
			view.Filter += view_Filter;
			view.View.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Ascending));
			QuickExperimentNameList.ItemsSource = view.View;
		}

		private void quickExper_ManagerItem_Click(object sender, RoutedEventArgs e)
		{
			QuickExperimentManageWindow quickExperimentManageWindow = new QuickExperimentManageWindow();
			quickExperimentManageWindow.Owner = Utility.GetMainWindow();
			quickExperimentManageWindow.OnManageChanged += window_OnManageChanged;
			bool flag = quickExperimentManageWindow.ShowDialog() == true;
		}

		private void window_OnManageChanged(object sender, EventArgs e)
		{
			RefreshList();
		}

		private void view_Filter(object sender, FilterEventArgs e)
		{
			bool accepted = false;
			QuickExperimentData quickExperimentData = e.Item as QuickExperimentData;
			if (quickExperimentData != null && quickExperimentData.IsShowInCanvas)
			{
				accepted = true;
			}
			e.Accepted = accepted;
		}

		public void RefreshList(bool refreshSelected = true)
		{
			view.View.Refresh();
			if (refreshSelected && QuickExperimentNameList.Items.Count > 0 && QuickExperimentNameList.SelectedItem == null)
			{
				QuickExperimentNameList.SelectedItem = QuickExperimentNameList.Items[0];
			}
		}

		private void ListBoxItem_Click(object sender, MouseButtonEventArgs e)
		{
			if (!(sender is QuickExperimentControl))
			{
				return;
			}
			QuickExperimentControl quickExperimentControl = sender as QuickExperimentControl;
			if (quickExperimentControl != null)
			{
				QuickExperimentData quickData = quickExperimentControl.QuickData;
				if (quickData != null)
				{
					OpenExperiment(quickData);
				}
			}
		}

		private void OpenExperiment(QuickExperimentData qd)
		{
			QuickExperimentManageChangedEventArgs quickExperimentManageChangedEventArgs = new QuickExperimentManageChangedEventArgs(OnQuickExperimentManageChangedRoutedEvent, this);
			quickExperimentManageChangedEventArgs.type = ManageChangedType.Open;
			quickExperimentManageChangedEventArgs.qd = qd;
			RaiseEvent(quickExperimentManageChangedEventArgs);
		}

		private void quickExper_DeleteItem_Click(object sender, RoutedEventArgs e)
		{
			if (QuickExperimentNameList.SelectedItems.Count == 1)
			{
				QuickExperimentData quickExperimentData = QuickExperimentNameList.SelectedItem as QuickExperimentData;
				if (quickExperimentData != null)
				{
					quickExperimentData.IsShowInCanvas = false;
				}
				RefreshList();
			}
		}

		private void quickExper_OpenItem_Click(object sender, RoutedEventArgs e)
		{
			if (QuickExperimentNameList.SelectedItems.Count == 1)
			{
				QuickExperimentData qd = QuickExperimentNameList.SelectedItem as QuickExperimentData;
				OpenExperiment(qd);
			}
		}
	}
}
